<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: songaimin@clcw.com.cn  Blog:http://blog.csdn.net/samxx8        |
// +------------------------------------------------------------------------+
//
namespace AuctionCore\Other;

use AuctionCore\System\AuctionException;

/**
 * 代金券
 * @author       Eric Lee
 */
class Coupon extends BaseCoupon
{
    private $dealer_id = null;
    private $coupon_id = null;

    /**
     * 构造
     * @param    int $dealer_id 车商id
     * @return   void
     */
    public function __construct($dealer_id, $coupon_id = '')
    {
        $this->dealer_id = $dealer_id;
        $this->coupon_id = $coupon_id;
    }

    /**
     * 生成代金券
     * 返回一组代金券号。
     * @param    array $sets 生成设置
     * [代金券类型，数量，卷面金额，有效起始日期，有效结束日期]
     * @return   array
     */
    public function gen($sets)
    {
        if (empty($sets['amount'])) {
            AuctionException::make_throw('800002');
        }

        $coupon_ids = array();
        for ($i = 0; $i < $sets['num']; $i++) {
            $data = array();
            $data['emp_id'] = $sets['emp_id'];
            $data['title'] = $sets['title'];
            $data['type'] = $sets['type'];
            $data['amount'] = $sets['amount'];
            $data['start_date'] = $sets['start_date'];
            $data['end_date'] = $sets['end_date'];
            $data['remark'] = $sets['remark'];
            $data['createtime'] = date('Y-m-d H:i:s');
            $coupon_ids[] = M('coupon')->add($data);
        }
        //emp_id
        //\Think\Log::record()
        return $coupon_ids;
    }

    /**
     * 发给车商代金券
     * @param    int $dealer_id 车商id
     * @param    string $coupon_id 代金券编号
     * @return   int
     */
    public function asgin($data)
    {
        if (empty($data['dealer_id'])) {
            AuctionException::make_throw('500001');
        }
        $data['coupon_id'] = $this->coupon_id;
        $id = M('dealer_coupon')->add($data);
        if ($id) {
            //\Think\Log::record()
        }
        return $id;

    }

    /*
     * 车商代金券统计
     * @return Array
     * */
    public function coupon_stat()
    {
        $stat = array();
        //条件
        $map = array();
        $map['dealer_id'] = array('eq', $this->dealer_id);
        $map['use_status'] = array('eq', '0');
        $map['end_date'] = array('egt', date('Y-m-d'));
        //当前代金券金额SELECT SUM(amount) AS s FROM au_dealer_coupon WHERE dealer_id = '55' AND STATUS= '1'
        $stat['cur_amount_sum'] = M('dealer_coupon')->where($map)->sum('amount');

        //当前可用代金券数量 SELECT   COUNT(*) AS cnt FROM  au_dealer_coupon WHERE dealer_id = '55'   AND STATUS = '1'
        $stat['available_num'] = M('dealer_coupon')->where($map)->count();
        //100元代金券5张   SELECT   COUNT(*) AS cnt, amount FROM  au_dealer_coupon WHERE dealer_id = '55'   AND STATUS = '1' GROUP BY amount
        $res = M('dealer_coupon')->field("COUNT(*) AS cnt, amount")->where($map)->group("amount")->select();
        $arr = array();
        foreach ($res as $k => $v) {
            $arr[$v['amount']] = $v['cnt'];
        }
        $stat['amount_stat'] = $arr;
        //500元代金券0张
        //本月即将到期的代金券：5张 SELECT   COUNT(*) AS cnt FROM  au_dealer_coupon WHERE dealer_id = '55'   AND STATUS = '0'  AND end_date LIKE '2015-11%'
        $map['end_date'] = array(array('egt', date('Y-m-d')), array('elt', date('Y-m-d', strtotime(date('Y-m-01', strtotime('+1 month'))) - 24 * 3600)));
        $res = M('dealer_coupon')->field("COUNT(*) AS cnt")->where($map)->find();
        $stat['expiring'] = $res['cnt'];

        return $stat;
    }

    /**
     * 获取优惠券数量
     * @param $params
     * @return mixed
     */
    public function get_coupon_count($params)
    {
        $map = array();
        $map['dealer_id'] = array('eq', $this->dealer_id);

        switch ($params['type']) {
            case '1':
                $map['use_status'] = array('eq', '0');
                $map['end_date'] = array('egt', date('Y-m-d'));
                break;
            case '2':
                $map['use_status'] = array('eq', '1');
                break;
            case '3':
                $map['use_status'] = array('neq', '1');
                $map['end_date'] = array('lt', date('Y-m-d'));
                break;
            default:
                break;
        }

        return M('dealer_coupon')->where($map)->count();
    }

}
